package com.xframe.domain.sys;

import com.xframe.domain.BaseModel;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.xframe.xdal.core.annotation.*;

import com.xframe.xdal.core.constant.AutoFillType;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

/**
 * 系统对象的命令表
 *
 * @author xufeng
 * @version 1.0
 * @date 2023-03-28
 */
@Data
@XTable(value = "SYS_COMMAND", label = "命令")
@XSelectItem(keyName = "commandCode",codeName = "commandCode",textName = "commandName")
public class SysCommand extends BaseModel implements Comparable<SysCommand> {
    /**
     * 命令ID
     */
    @AutoFill(AutoFillType.Snowflake)
    @XColumn(name = "COMMAND_ID", primaryKeyFlag = true,desc = "命令ID")
    private Long commandId;

    /**
     * 命令编码 唯一
     */
    @XQueryCondition
    @XGridTableRef
    @XColumn(uniqueFlag = true,desc = "命令编码",length = 100)
    private String commandCode;

    /**
     * 命令名称描述
     */
    @XColumn(desc = "命令名称",length = 200)
    private String commandName;

    /**
     * 命令对应的类名称
     */
    @XColumn(desc = "类名称",length = 500)
    private String className;

    /**
     * 命令对象的配置字符串
     */
    @XColumn(desc = "配置字符串",length = 1000)
    private String configStr;


    @Getter(AccessLevel.NONE)
    @Setter(AccessLevel.NONE)
    @JsonIgnore
    private Object commandInstance;

    public <T> T getCommandInstance(){
        if(commandInstance == null) {
            try {
                commandInstance = (T) Class.forName(this.getClassName()).newInstance();
            } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
                e.printStackTrace();
            }
        }
        return (T) commandInstance;
    }

    @Override
    public int compareTo(SysCommand sysCommand) {
        return this.getCommandId().compareTo(sysCommand.commandId);
    }

    @Override
    public Object getMapkey(){
        return this.commandCode;
    }
}
